import * as api from '../api/api';
import request from '../utils/request';
import { pageSize } from '../utils/common';

export default {
    namespace: 'storeSetting',
    state: {
        storeSetting: {},
        storeSettings: { totalCount: 0, items: [] },
        storeSettingsSearchs: { pageIndex: 1, pageSize, keywords: "" },
        advertSetting: {},
        advertSettings: { totalCount: 0, items: [] },
        advertSettingsSearchs: { pageIndex: 1, pageSize, keywords: "" },
        homeSetting: {},
        homeSettings: { totalCount: 0, items: [] },
        homeSettingsSearchs: { pageIndex: 1, pageSize, keywords: "" }
    },
    effects: {
        /**
         * 获取设置列表
         * @param param0 
         * @param param1
         */
        *getStoreSettings({ payload }, { call, put }) {
            const { keywords, pageIndex, pageSize } = payload;
            const formKeywords = keywords, formSkipCount = (pageIndex - 1) * pageSize, formMaxResultCount = pageSize;
            const storeSettings: api.AbpApplicationServicesDtoPagedResultDtoSingleUserMallStoreSettingsVOStoreSettingVO =
                yield request(api.ManagestoreSettingManageApiFetchParamCreactor.managestoreSettingManageGetStoreSettingsBySearchAsync
                    ({
                        formKeywords, formSkipCount, formMaxResultCount
                    }))
            yield put({ type: 'save', payload: { storeSettings: storeSettings, storeSettingsSearchs: { keywords, pageIndex, pageSize } } });
        },

        /**
         * 获取广告列表
         * @param param0 
         * @param param1
         */
        *getAdvertSettings({ payload }, { call, put }) {
            const { keywords, pageIndex, pageSize } = payload;
            const formKeywords = keywords, formSkipCount = (pageIndex - 1) * pageSize, formMaxResultCount = pageSize;
            const advertSettings: api.AbpApplicationServicesDtoPagedResultDtoSingleUserMallStoreSettingsVOAdvertSettingVO =
                yield request(api.ManagestoreSettingManageApiFetchParamCreactor.managestoreSettingManageGetAdvertSettingsBySearchAsync
                    ({
                        formKeywords, formSkipCount, formMaxResultCount
                    }))
            yield put({ type: 'save', payload: { advertSettings: advertSettings, advertSettingsSearchs: { keywords, pageIndex, pageSize } } });
        },

        /**
         * 获取首页设置列表
         * @param param0 
         * @param param1
         */
        *getHomeSettings({ payload }, { call, put }) {
            const { keywords, pageIndex, pageSize } = payload;
            const formKeywords = keywords, formSkipCount = (pageIndex - 1) * pageSize, formMaxResultCount = pageSize;
            const homeSettings: api.AbpApplicationServicesDtoPagedResultDtoSingleUserMallStoreSettingsVOHomeSettingVO =
                yield request(api.ManagestoreSettingManageApiFetchParamCreactor.managestoreSettingManageGetHomeSettingsBySearchAsync
                    ({
                        formKeywords, formSkipCount, formMaxResultCount
                    }))
            yield put({ type: 'save', payload: { homeSettings: homeSettings, homeSettingsSearchs: { keywords, pageIndex, pageSize } } });
        },
        /**
         * 获取设置详情
         * @param param0 
         * @param param1 
         */
        *getStoreSetting({ payload }, { call, put }) {
            const formId = payload.id;
            const storeSetting: api.SingleUserMallStoreSettingsVOStoreSettingVO =
                yield request(api.ManagestoreSettingManageApiFetchParamCreactor.managestoreSettingManageGetStoreSettingAsync({
                    formId,
                }))
            yield put({ type: 'save', payload: { storeSetting: storeSetting } });
        },
         /**
         * 获取广告详情
         * @param param0 
         * @param param1 
         */
        *getAdvertSetting({ payload }, { call, put }) {
            const formId = payload.id;
            const advertSetting: api.SingleUserMallStoreSettingsVOAdvertSettingVO =
                yield request(api.ManagestoreSettingManageApiFetchParamCreactor.managestoreSettingManageGetAdvertSettingAsync({
                    formId,
                }))
            yield put({ type: 'save', payload: { advertSetting: advertSetting } });
        },
        /**
         * 获取首页设置详情
         * @param param0 
         * @param param1 
         */
        *getHomeSetting({ payload }, { call, put }) {
            const formId = payload.id;
            const homeSetting: api.SingleUserMallStoreSettingsVOHomeSettingVO =
                yield request(api.ManagestoreSettingManageApiFetchParamCreactor.managestoreSettingManageGetHomeSettingAsync({
                    formId,
                }))
            yield put({ type: 'save', payload: { homeSetting: homeSetting } });
        },
        /**
        * 删除商城设置
        * @param param0 
        * @param param1 
        */
        *remove({ payload }, { call, put, select }) {
            const { form, callback } = payload;
            const { storeSettingsSearchs } = yield select(({ storeSetting }) => storeSetting);
            yield request(api.ManagestoreSettingManageApiFetchParamCreactor.managestoreSettingManageRemoveAsync(
                { form }
            ));
            yield put({ type: 'getStoreSettings', payload: { ...storeSettingsSearchs } });
            callback && callback(true);
        },
        /**
        * 删除广告设置
        * @param param0 
        * @param param1 
        */
        *removeAdvert({ payload }, { call, put, select }) {
            const { form, callback } = payload;
            const { advertSettingsSearchs } = yield select(({ storeSetting }) => storeSetting);
            yield request(api.ManagestoreSettingManageApiFetchParamCreactor.managestoreSettingManageRemoveAdvertAsync(
                { form }
            ));
            yield put({ type: 'getAdvertSettings', payload: { ...advertSettingsSearchs } });
            callback && callback(true);
        },
        /**
        * 删除首页设置
        * @param param0 
        * @param param1 
        */
       *removeHome({ payload }, { call, put, select }) {
        const { form, callback } = payload;
        const { homeSettingsSearchs } = yield select(({ storeSetting }) => storeSetting);
        yield request(api.ManagestoreSettingManageApiFetchParamCreactor.managestoreSettingManageRemoveHomeAsync(
            { form }
        ));
        yield put({ type: 'getHomeSettings', payload: { ...homeSettingsSearchs } });
        callback && callback(true);
    },
        /**
        * 保存设置
        * @param param0 
        * @param param1 
        */
        *saveStoreSetting({ payload }, { call, put, select }) {
            const { form, callback } = payload;
            const { storeSettingsSearchs } = yield select(({ storeSetting }) => storeSetting);
            yield request(api.ManagestoreSettingManageApiFetchParamCreactor.managestoreSettingManageSaveStoreSettingAsync(
                { form }
            ));
            yield put({ type: 'getStoreSettings', payload: { ...storeSettingsSearchs } });
            callback && callback(true);
        },
        /**
        * 保存广告设置
        * @param param0 
        * @param param1
        */
        *saveAdvertSetting({ payload }, { call, put, select }) {
            const { form, callback } = payload;
            const { advertSettingsSearchs } = yield select(({ storeSetting }) => storeSetting);
            yield request(api.ManagestoreSettingManageApiFetchParamCreactor.managestoreSettingManageSaveAdvertSettingAsync(
                { form }
            ));
            yield put({ type: 'getAdvertSettings', payload: { ...advertSettingsSearchs } });
            callback && callback(true);
        },
        /**
        * 保存广告设置
        * @param param0 
        * @param param1
        */
       *saveHomeSetting({ payload }, { call, put, select }) {
        const { form, callback } = payload;
        const { homeSettingsSearchs } = yield select(({ storeSetting }) => storeSetting);
        yield request(api.ManagestoreSettingManageApiFetchParamCreactor.managestoreSettingManageSaveHomeSettingAsync(
            { form }
        ));
        yield put({ type: 'getHomeSettings', payload: { ...homeSettingsSearchs } });
        callback && callback(true);
    }

    },
    reducers: {
        save(state, action) {
            return { ...state, ...action.payload };
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, search }) => {
                switch (pathname) {
                    case '/storesetting/storeSettings':
                        dispatch({ type: 'getStoreSettings', payload: { pageIndex: 1, pageSize, keywords: "" } });
                        break;
                    case '/storesetting/advertSettings':
                        dispatch({ type: 'getAdvertSettings', payload: { pageIndex: 1, pageSize, keywords: "" } });
                        break;
                     case '/storesetting/homeSettings':
                        dispatch({ type: 'getHomeSettings', payload: { pageIndex: 1, pageSize, keywords: "" } });
                        break;
                }
            });
        },
    },
};